﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaFramework;
using LuaInterface;

namespace LuaFramework {
    /// <summary>
    /// 可以用多个相同的，建议使用这个
    /// </summary>
    public class LuaComponent : MonoBehaviour {

		public AssetsRef assetsRef = null;
		[Space]
        public bool useRequireLua = true;
        public string luaPath = null;

		internal string _name;

        internal LuaTable _table;
		public LuaTable luaTable{
			get{
				return _table;
			}
		}

        public static LuaTable Get(GameObject go , LuaTable table)
        {
            LuaComponent[] cmps = go.GetComponents(typeof(LuaComponent)) as LuaComponent[];
            foreach (LuaComponent cmp in cmps)
            {
                string mat1 = table.ToString();
                string mat2 = cmp._table.GetMetaTable().ToString();
                if (mat1 == mat2)
                    return cmp._table;
            }
            return null;
        }
    	
        void Awake(){
			LuaManager _instance = LuaManager.Instance;
            if (!string.IsNullOrEmpty(luaPath))
            {
                _name = System.IO.Path.GetFileName(luaPath);
                if(useRequireLua){
					_table = LuaManager.Instance.RequireFile(luaPath);
                }else{
					LuaManager.Instance.DoFile(luaPath);
					_table = LuaManager.Instance.Lua.GetTable(_name);
                }

                if (_table != null)
                {
                    LuaFunction func = _table.GetLuaFunction("New");
                    if (func != null)
                    {
                        _table = func.Invoke<LuaTable>();
                        func.Dispose();
                        func = null;
                    }
                    else
                    {
                        _table = null;
                    }

                    if (_table!=null)
                    {
                        func = _table.GetLuaFunction("Awake");
                        if (func != null)
                        {
							func.Call(_table,gameObject,assetsRef);
                            func.Dispose();
                            func = null;
                        }
                    }
                }
            }
        }

        void OnEnable() {
			if (!string.IsNullOrEmpty(_name) && _table!=null)
            {
                LuaFunction func = _table.GetLuaFunction("OnEnable");
                if (func != null)
                {   
                    func.Call(_table);
                    func.Dispose();
                    func = null;
                }
            }
        }

        // Use this for initialization
        void Start () {
            if (_table != null && !string.IsNullOrEmpty(_name))
            {
                LuaFunction func = _table.GetLuaFunction("Start");
                if (func != null)
                {
                    func.Call(_table);
                    func.Dispose();
                    func = null;
                }
            }

        }

        void OnDisable() {
            if (!string.IsNullOrEmpty(_name))
            {
				if (!LuaManager.IsInited || _table==null) return;

                LuaFunction func = _table.GetLuaFunction("OnDisable");
                if (func != null)
                {
                    func.Call(_table);
                    func.Dispose();
                    func = null;
                }
            }
        }

		public void CallLuaFun(string luaFunc){
			if (!string.IsNullOrEmpty(_name))
			{
				if (!LuaManager.IsInited || _table==null) return;
				LuaManager mgr = LuaManager.Instance;

				LuaFunction func = _table.GetLuaFunction(luaFunc);
				if (func != null)
				{
					func.Call(_table);
					func.Dispose();
					func = null;
				}
			}
		}

		void OnApplicationFocus(bool hasFocus)
		{
			if (!string.IsNullOrEmpty(_name))
			{
				if (!LuaManager.IsInited || _table==null) return;

				LuaFunction func = _table.GetLuaFunction("OnApplicationFocus");
				if (func != null)
				{
					func.Call(_table,hasFocus);
					func.Dispose();
					func = null;
				}
			}
		}

		void OnApplicationPause(bool pauseStatus)
		{
			if (!string.IsNullOrEmpty(_name))
			{
				if (!LuaManager.IsInited || _table==null) return;

				LuaFunction func = _table.GetLuaFunction("OnApplicationPause");
				if (func != null)
				{
					func.Call(_table,pauseStatus);
					func.Dispose();
					func = null;
				}
			}
		}

        void OnDestroy(){
            if (!string.IsNullOrEmpty(_name))
            {
                if (!LuaManager.IsInited) return;

                if (_table != null)
                {
                    LuaFunction func = _table.GetLuaFunction("OnDestroy");
                    if (func != null)
                    {
                        func.Call(_table);
                        func.Dispose();
                        func = null;
                    }
                    _table.Dispose();
                }
            }
        }
    }
}